package com.wht.factory;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author wanghaotian
 * time:2022/11/29,0029 10:27
 * description:
 */

public class MyFactory{
    private static Map<String,Object> map = new HashMap<String, Object>();
    static {
        try {
            Properties properties = new Properties();
            properties.load(MyFactory.class.getClassLoader().getResourceAsStream("bean.properties"));
//            System.out.println(MyFactory.class);
//            System.out.println(MyFactory.class.getClassLoader());
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()){
                String key = (String) enumeration.nextElement();
//                System.out.println(key);
                String property = properties.getProperty(key);
                Class<?> aClass = Class.forName(property);
                Object o = aClass.newInstance();
                map.put(key, o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getBean(String key){
        return map.get(key);
    }

    public static void main(String[] args) {
        Object userDao = MyFactory.getBean("userDao");
        System.out.println(userDao);
    }
}
